//
//  UIViewController+RefreshTag.m
//  
//
//  Created by wjxfhxy on 16/7/7.
//
//

#import "UIViewController+RefreshTag.h"

@implementation UIViewController (RefreshTag)

//-(void)setViewControllerRefreshTag:(NSUInteger)viewControllerRefreshTag
//{
//    [self addViewControllerRefreshTag:viewControllerRefreshTag cacheData:nil];
//}

-(NSUInteger)viewControllerRefreshTag
{
    return [(NSNumber*)[self associatedValueForKey:"refreshtag"] integerValue];
}

-(id)cacheData:(UIViewControllerRefreshTagType)refreshTag
{
    if(refreshTag == UIViewControllerRefreshTagTypeInfoCacheRefresh)
        return [self infoAttachData];
    if(refreshTag == UIViewControllerRefreshTagTypeListCacheRefresh)
        return [self listAttachData];
    return nil;
}

-(NSArray*)listCacheData
{
    NSArray *ar = [self cacheData:UIViewControllerRefreshTagTypeListCacheRefresh];
    return ar == nil ? @[] : ar;
}

-(id)infoCacheData
{
    return [self cacheData:UIViewControllerRefreshTagTypeInfoCacheRefresh];
}

-(id)infoAttachData
{
    return [self associatedValueForKey:"infoattachdata"];
}

-(id)listAttachData
{
    return [self associatedValueForKey:"listattachdata"];
}

-(void)setInfoAttachData:(id)infoAttachData
{
    [self setAssociatedRetainNonatomicValue:infoAttachData withKey:"infoattachdata"];
}

-(void)setListAttachData:(id)listAttachData
{
    [self setAssociatedRetainNonatomicValue:listAttachData withKey:"listattachdata"];
}

-(void)addViewControllerRefreshTag:(UIViewControllerRefreshTagType)refreshTag cacheData:(id)cacheData
{
    if(refreshTag == UIViewControllerRefreshTagTypeNone)
    {
        [self setAssociatedRetainNonatomicValue:@(refreshTag) withKey:"refreshtag"];
        self.infoAttachData = nil;
        self.listAttachData = nil;
        return;
    }
    
    NSUInteger cur = self.viewControllerRefreshTag;
    
    if(refreshTag == UIViewControllerRefreshTagTypeInfoCacheRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeInfoLocalRefresh) == UIViewControllerRefreshTagTypeInfoLocalRefresh ||
           (cur & UIViewControllerRefreshTagTypeInfoNetRefresh) == UIViewControllerRefreshTagTypeInfoNetRefresh )
        {
            return;
        }
        else
        {
            self.infoAttachData = cacheData;
        }
    }
    else if(refreshTag == UIViewControllerRefreshTagTypeInfoLocalRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeInfoCacheRefresh) == UIViewControllerRefreshTagTypeInfoCacheRefresh)
            cur ^= UIViewControllerRefreshTagTypeInfoCacheRefresh;
        self.infoAttachData = nil;
        if((cur & UIViewControllerRefreshTagTypeInfoNetRefresh) == UIViewControllerRefreshTagTypeInfoNetRefresh )
        {
            return;
        }
    }
    else if(refreshTag == UIViewControllerRefreshTagTypeInfoNetRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeInfoCacheRefresh) == UIViewControllerRefreshTagTypeInfoCacheRefresh)
            cur ^= UIViewControllerRefreshTagTypeInfoCacheRefresh;
        if((cur & UIViewControllerRefreshTagTypeInfoLocalRefresh) == UIViewControllerRefreshTagTypeInfoLocalRefresh)
            cur ^= UIViewControllerRefreshTagTypeInfoLocalRefresh;
        self.infoAttachData = nil;
    }
    
    else if(refreshTag == UIViewControllerRefreshTagTypeListCacheRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeListLocalRefresh) == UIViewControllerRefreshTagTypeListLocalRefresh ||
           (cur & UIViewControllerRefreshTagTypeListNetRefresh) == UIViewControllerRefreshTagTypeListNetRefresh )
        {
            return;
        }
        else
        {
            self.listAttachData = cacheData;
        }
    }
    else if(refreshTag == UIViewControllerRefreshTagTypeListLocalRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeListCacheRefresh) == UIViewControllerRefreshTagTypeListCacheRefresh)
            cur ^= UIViewControllerRefreshTagTypeListCacheRefresh;
        self.listAttachData = nil;
        if((cur & UIViewControllerRefreshTagTypeListNetRefresh) == UIViewControllerRefreshTagTypeListNetRefresh )
        {
            return;
        }
    }
    else if(refreshTag == UIViewControllerRefreshTagTypeListNetRefresh)
    {
        if((cur & UIViewControllerRefreshTagTypeListCacheRefresh) == UIViewControllerRefreshTagTypeListCacheRefresh)
            cur ^= UIViewControllerRefreshTagTypeListCacheRefresh;
        if((cur & UIViewControllerRefreshTagTypeListLocalRefresh) == UIViewControllerRefreshTagTypeListLocalRefresh)
            cur ^= UIViewControllerRefreshTagTypeListLocalRefresh;
        self.listAttachData = nil;
    }
    
    cur |= refreshTag;
    
    [self setAssociatedRetainNonatomicValue:@(cur) withKey:"refreshtag"];
}

-(void)addViewControllerRefreshTag:(UIViewControllerRefreshTagType)refreshTag
{
    [self addViewControllerRefreshTag:refreshTag cacheData:nil];
}

-(void)setViewControllerListCacheData:(id)cacheData
{
    [self addViewControllerRefreshTag:UIViewControllerRefreshTagTypeListCacheRefresh cacheData:cacheData];
}

-(void)setViewControllerInfoCacheData:(id)cacheData
{
    [self addViewControllerRefreshTag:UIViewControllerRefreshTagTypeInfoCacheRefresh cacheData:cacheData];
}

@end
